var url = require('url');
var template = require('templateConfig');
var main = require('main');
var form = require('form');
var module= require('module');

//模板配置
var tplArr = [];
var confirmTpl =require('raw!../template/order-confirm.txt'); 
tplArr['confirm'] = template.compile(confirmTpl.replace(/^\s*|\s*$/g, "")); 
 
function Index(){
    _this = this;
    this.main = function(){
        main.event(this,'tap','i-click'); 
        this.init();
    }     

    this.getContent= function(getUrl, postUrl, addressUrl, ids, addrid){
        main.post(getUrl, {cart_id: ids}, function(rest){   
            var ret= rest;
            if(ret.code!= 1) {
                return false;
            }; 
        	var dataPrice= {
        		tType: "price",
        		price: "0.00", 
        	}; 
        	var dataAddress= {
        		tType: "address",
        		address: " ",
        		link: _this.jumpUrl+ "?id="+ _this.ids
        	}
        	var dataOrder= {
        		tType: "orderList",
        		dataList: []
        	}
            var p= 0;
            for(var n in ret.data.cart){
                var _json= ret.data.cart[n];
                var _data= {
                        "cart_id": _json.cart_id,
                        "id": _json.good_id,
                        "title":_json.good_name,
                        //"attr": [],
                        "num":_json.good_sku,
                        "spec":_json.colour+ ","+ _json.cup+ ","+ _json.size, 
                        "price": _json.shop_price,
                        "quantity": _json.good_number,
                        "image":_json.small_image,
                        "n":_json.good_number,
                        "store":ret.data.store_name,
                };
                p+= parseFloat(_json.shop_price)*parseFloat(_json.good_number);
                dataOrder.dataList.push(_data); 
            }       
        	var data= {
        		tType: "content",
        		price: tplArr['confirm'](dataPrice),
        		address: tplArr['confirm'](dataAddress),
        		orderList: module.ordersGoodsInfo(dataOrder.dataList),
        		click: "confirmOrder", 
                postUrl: postUrl,
                cart: _this.ids
        	}
        	$(".content").html(tplArr['confirm'](data));
            $(".price span").html("￥"+p);             
            _this.getAddress(addressUrl, addrid);
        })
    } 
    //获取地址
    this.getAddress= function(addressUrl, addrid){
        var obj= {};
        if(!!addrid){
            obj.address_id=addrid;
        }
        main.post(addressUrl, obj, function(rest){ console.info(rest)
            if(rest.code==1){
                var ret= rest.data; 
                $("#address").html(ret.province_name+" "+ret.city_name+ret.district_name+ret.address+" "+ret.name+" "+ret.mobile);
                $("#address_id").val(ret.address_id);
            }
        })
    }

    //提交订单
    this.confirmOrder= function(ths){
        form.ajaxSubmit(".postForm",_this,ths);
    }
    this.formCallback= function(ret){ 
        console.info(ret)
        if(ret.code== 1){             
            var _url=_this.payUrl+ "?order_id="+ret.data.order_id +"&order_sn="+ret.data.order_sn;
            console.info(_url)
            window.location.href= _url;
        }
    }

    this.init= function(){ 
        this.ids= main.GetQueryString("id");
        this.addrid= main.GetQueryString("address_id");
        this.getUrl= url.postConfirmOrder;
        this.addressUrl= url.postSelectAddress;
        this.postUrl= url.postPayOrder;
        this.jumpUrl= "address";
        this.payUrl= "http://protest1.woaap.com/pay/wechat"
        this.getContent(this.getUrl, this.postUrl, this.addressUrl, this.ids, this.addrid);  
    }
    return this.main();
};

var index = new Index();